import 'package:flutter_plugin/base/controller/base_controller.dart';
import 'package:flutter_plugin/constant/sp_key.dart';
import 'package:flutter_plugin/util/sputil.dart';

import '../../../ext/get_extension.dart';
import '../../../global/global_controller.dart';
import '../../../net/api_respository.dart';
import '../../../net/base/NWMethod.dart';
import '../../../net/dio_manager.dart';
import '../../../net/http_url.dart';
import 'tab_mine_state.dart';

class TabMineLogic extends BaseController {
  final TabMineState state = TabMineState();

  @override
  void loadNet() {}

  @override
  void onInit() {
    super.onInit();
    state.phone = SPUtil.instance.get(SPKey.phone_input) ?? "";
    state.name = SPUtil.instance.get(SPKey.name) ?? "";
    state.avatar = SPUtil.instance.get(SPKey.avatar) ?? "";
    signedUrl();
  }

  void signedUrl() {
    String url = "${HttpUrl.signedUrl}${state.avatar}";
    var header = DioManager.headers();
    header["r_tag"] = state.avatar;
    ApiRepository.instance.request<String>(url, header: header, method: NWMethod.GET, success: (it) {
      state.headImg.value = it;
    }, error: (e) {
      showToast(e.message ?? "获取头像失败");
    });
  }

  void logout() {
    GetExtension.showCommonDialog(
      title: "确认退出？",
      content: "请再次确认退出登录，退出后，您的守护家人不能第一时间关注您的状况。",
      posiText: "确定",
      posiTap: () {
        ApiRepository.instance.request<String>(HttpUrl.logout, method: NWMethod.GET);
        GlobalController.to.logout();
      },
    );
  }
}
